Skip to content

fix(sidebar): prevent null responses from sidebar API endpoints#974

Open
DHRUVI5674 wants to merge 1 commit intomattermost:masterfrom
DHRUVI5674:master
Open

fix(sidebar): prevent null responses from sidebar API endpoints#974
DHRUVI5674 wants to merge 1 commit intomattermost:masterfrom
DHRUVI5674:master

Conversation

@DHRUVI5674
Copy link

@DHRUVI5674 DHRUVI5674 commented Mar 2, 2026

Fixes #235

Summary

Sidebar API endpoints sometimes returned null when GitHub API calls failed or returned unexpected data. This caused the Mattermost webapp sidebar to crash.

Changes

  • Return empty slices instead of nil in getUnreadsData
  • Ensure getSidebarData returns default SidebarContent on error
  • Added safety checks in sidebar API response
  • Improved JSON response handling to avoid null values

Testing

  • Tested locally with GitHub API failure simulation
  • Verified sidebar loads without crashing
  • Confirmed empty data shows correctly

Result

The sidebar no longer crashes when API endpoints return null or fail unexpectedly.

Type of change

  • Bug fix
  • New feature
  • Breaking change

Summary by CodeRabbit

Release Notes

  • Bug Fixes

    • Improved error handling and response consistency for API endpoints when requests fail.
    • Fixed response payload structure to ensure non-nil returns in error scenarios.
  • Refactor

    • Enhanced logging for better troubleshooting of failed operations.
    • Standardized error handling across API response functions for more predictable behaviour.

@DHRUVI5674 DHRUVI5674 requested a review from a team as a code owner March 2, 2026 09:09
@mattermost-build
Copy link
Contributor

Hello @DHRUVI5674,

Thanks for your pull request! A Core Committer will review your pull request soon. For code contributions, you can learn more about the review process here.

Per the Mattermost Contribution Guide, we need to add you to the list of approved contributors for the Mattermost project.

Please help complete the Mattermost contribution license agreement?
Once you have signed the CLA, please comment with /check-cla and confirm that the CLA check is green.

This is a standard procedure for many open source projects.

Please let us know if you have any questions.

We are very happy to have you join our growing community! If you're not yet a member, please consider joining our Contributors community channel to meet other contributors and discuss new opportunities with the core team.

@marianunez
Copy link
Member

@coderabbitai full review

@coderabbitai
Copy link

coderabbitai bot commented Mar 4, 2026

✅ Actions performed

Full review triggered.

@marianunez marianunez added the 2: Dev Review Requires review by a core committer label Mar 4, 2026
@coderabbitai
Copy link

coderabbitai bot commented Mar 4, 2026

📝 Walkthrough

Walkthrough

The change modifies error handling in the API layer to gracefully degrade on failures from GitHub by returning empty data structures instead of null or propagating errors, ensuring the webapp remains functional during temporary outages.

Changes

Cohort / File(s) Summary
API Error Handling
server/plugin/api.go
Modified writeJSON to set Content-Type header and normalize nil responses to empty maps; updated getUnreadsData, getSidebarData, and getSidebarContent to return empty default structures on GitHub API failures instead of propagating errors, improving resilience against transient outages.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main change: preventing null responses from sidebar API endpoints, which directly addresses the core issue.
Linked Issues check ✅ Passed The code changes address the core requirements from issue #235: returning empty slices instead of nil, ensuring non-null responses on errors, and adding safety checks to prevent null API responses.
Out of Scope Changes check ✅ Passed All changes in server/plugin/api.go are directly related to fixing null response handling in sidebar API endpoints as required by issue #235, with no unrelated modifications detected.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 golangci-lint (2.5.0)

Error: unknown linters: 'modernize', run 'golangci-lint help linters' to see the list of supported linters
The command is terminated due to an error: unknown linters: 'modernize', run 'golangci-lint help linters' to see the list of supported linters


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Nitpick comments (1)
server/plugin/api.go (1)

1045-1064: Consider: swallowing errors prevents the frontend from showing error state.

Per the relevant code snippets, the webapp's getSidebarContent action uses isAPIError(data) to distinguish between error responses and successful data. With this change, the backend always returns HTTP 200 with valid JSON, so isAPIError will never be true for sidebar content.

This means:

  • Users cannot distinguish between "GitHub API is down" vs "you have zero PRs/reviews"
  • The linked issue (#235) mentions the backend should "return a non-200 status if fetching from GitHub fails"

If this behaviour is intentional for resilience (as stated in PR objectives), consider at minimum adding a boolean field like partial: true or fetchError: true to the response so the frontend can optionally display a warning indicator.

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@server/plugin/api.go`:
- Around line 1068-1077: The build fails because logger.Logger has no Warn
method; update the defensive logging in the block that checks "if err != nil ||
sidebarContent == nil" to call c.Log.Warnf instead of c.Log.Warn, e.g., use
c.Log.Warnf("Sidebar content empty, returning defaults") so the call matches the
logger API; leave the nil-check and defaulting for sidebarContent (and note
getSidebarData still returns non-nil in current code) as a safeguard.
- Line 1048: Replace the non-existent call to c.Log.WithError(err).Warn by using
the existing Warnf pattern: call c.Log.WithError(err).Warnf with a formatted
message string (e.g., "Sidebar data fetch failed: %v") and pass the error or
relevant args so the logger compiles and mirrors other calls in this file (see
other uses of c.Log.WithError(...).Warnf).

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 3c1db2c0-eddf-42e4-9e43-f2b4efc06bcd

📥 Commits

Reviewing files that changed from the base of the PR and between ff976a6 and ac4e8cf.

📒 Files selected for processing (1)
  • server/plugin/api.go

if err != nil {
return nil, err
}
c.Log.WithError(err).Warn("Sidebar data fetch failed")
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

Build failure: Warn method does not exist on logger.Logger.

The static analysis indicates that logger.Logger has no method Warn. Use Warnf instead, which is consistent with other logging calls in this file (e.g., line 721).

🐛 Proposed fix
-    c.Log.WithError(err).Warn("Sidebar data fetch failed")
+    c.Log.WithError(err).Warnf("Sidebar data fetch failed")
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
c.Log.WithError(err).Warn("Sidebar data fetch failed")
c.Log.WithError(err).Warnf("Sidebar data fetch failed")
🧰 Tools
🪛 GitHub Check: plugin-ci / test

[failure] 1048-1048:
c.Log.WithError(err).Warn undefined (type logger.Logger has no field or method Warn)

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@server/plugin/api.go` at line 1048, Replace the non-existent call to
c.Log.WithError(err).Warn by using the existing Warnf pattern: call
c.Log.WithError(err).Warnf with a formatted message string (e.g., "Sidebar data
fetch failed: %v") and pass the error or relevant args so the logger compiles
and mirrors other calls in this file (see other uses of
c.Log.WithError(...).Warnf).

Comment on lines +1068 to +1077
if err != nil || sidebarContent == nil {
c.Log.Warn("Sidebar content empty, returning defaults")

sidebarContent = &SidebarContent{
PRs: []*graphql.GithubPRDetails{},
Reviews: []*graphql.GithubPRDetails{},
Assignments: []*github.Issue{},
Unreads: []*FilteredNotification{},
}
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

Build failure: Warn method does not exist on logger.Logger.

Same issue as above - use Warnf instead of Warn.

Additionally, note that this defensive block is now effectively unreachable: getSidebarData always returns a non-nil SidebarContent and nil error after your changes. The check is still reasonable as a safeguard, but consider whether this logging path will ever execute.

🐛 Proposed fix
 if err != nil || sidebarContent == nil {
-    c.Log.Warn("Sidebar content empty, returning defaults")
+    c.Log.Warnf("Sidebar content empty, returning defaults")
 
     sidebarContent = &SidebarContent{
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if err != nil || sidebarContent == nil {
c.Log.Warn("Sidebar content empty, returning defaults")
sidebarContent = &SidebarContent{
PRs: []*graphql.GithubPRDetails{},
Reviews: []*graphql.GithubPRDetails{},
Assignments: []*github.Issue{},
Unreads: []*FilteredNotification{},
}
}
if err != nil || sidebarContent == nil {
c.Log.Warnf("Sidebar content empty, returning defaults")
sidebarContent = &SidebarContent{
PRs: []*graphql.GithubPRDetails{},
Reviews: []*graphql.GithubPRDetails{},
Assignments: []*github.Issue{},
Unreads: []*FilteredNotification{},
}
}
🧰 Tools
🪛 GitHub Check: plugin-ci / test

[failure] 1069-1069:
c.Log.Warn undefined (type logger.Logger has no field or method Warn)

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@server/plugin/api.go` around lines 1068 - 1077, The build fails because
logger.Logger has no Warn method; update the defensive logging in the block that
checks "if err != nil || sidebarContent == nil" to call c.Log.Warnf instead of
c.Log.Warn, e.g., use c.Log.Warnf("Sidebar content empty, returning defaults")
so the call matches the logger API; leave the nil-check and defaulting for
sidebarContent (and note getSidebarData still returns non-nil in current code)
as a safeguard.

Copy link
Member

@marianunez marianunez left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@DHRUVI5674 thank you for your contribution!

Please take a look at the lint failures and also sign the CLA.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

2: Dev Review Requires review by a core committer Contributor

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Handle case where sidebar API endpoints return null

3 participants